/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.pod.service.impl;

import me.zhengjie.modules.cspimp.pod.domain.Car;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.pod.repository.CarRepository;
import me.zhengjie.modules.cspimp.pod.service.CarService;
import me.zhengjie.modules.cspimp.pod.service.dto.CarDto;
import me.zhengjie.modules.cspimp.pod.service.dto.CarQueryCriteria;
import me.zhengjie.modules.cspimp.pod.service.mapstruct.CarMapper;
import me.zhengjie.utils.enums.CarTypeEnums;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CarServiceImpl implements CarService {

    private final CarRepository carRepository;
    private final CarMapper carMapper;

    @Override
    public Map<String,Object> queryAll(CarQueryCriteria criteria, Pageable pageable){
        Page<Car> page = carRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(carMapper::toDto));
    }

    @Override
    public List<CarDto> queryAll(CarQueryCriteria criteria){
        return carMapper.toDto(carRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public CarDto findById(Long id) {
        Car car = carRepository.findById(id).orElseGet(Car::new);
        ValidationUtil.isNull(car.getId(),"Car","id",id);
        return carMapper.toDto(car);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CarDto create(Car resources) {
        Long carTypeId = resources.getCarTypeId();
        if(null != carTypeId){
            CarTypeEnums carTypeEnums = CarTypeEnums.find(carTypeId);
            resources.setCarTypeCode(carTypeEnums.getCode());
            resources.setCarTypeName(carTypeEnums.getName());
        }
        return carMapper.toDto(carRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Car resources) {
        Car car = carRepository.findById(resources.getId()).orElseGet(Car::new);
        ValidationUtil.isNull( car.getId(),"Car","id",resources.getId());
        car.copy(resources);
        Long carTypeId = resources.getCarTypeId();
        if(null != carTypeId){
            CarTypeEnums carTypeEnums = CarTypeEnums.find(carTypeId);
            car.setCarTypeCode(carTypeEnums.getCode());
            car.setCarTypeName(carTypeEnums.getName());
        }

        carRepository.save(car);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            carRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CarDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CarDto car : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", car.getCreateTime());
            map.put("更新时间", car.getUpdateTime());
            map.put("创建者", car.getCreateBy());
            map.put("更新者", car.getUpdateBy());
            map.put("车辆名称", car.getName());
            map.put("车辆类型", car.getCarTypeName());
            map.put("车牌号", car.getCarNumber());
            map.put("驾驶员", car.getDriver());
            map.put("押运员", car.getSupercargo());
            map.put("运单信息", car.getWaybill());
            map.put("标签", car.getTag());
            map.put("图标", car.getIcon());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}